﻿Option Explicit On

Public Class frmFarpost
    Private params As Object
    Private is_run As Boolean
    Private Const SITE_URL As String = "http://komsomolsk-na-amure.farpost.ru/"
    Private adverts() As AdvertType
    Private cur_adv As Integer
    Public Const ADV_NAME As String = "farpost"
    Public file_name As String
    Public catalog_name As String
    Private h1 As String
    Private wbc As New System.Net.WebClient


    Private Sub frmFarpost_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cboCatalog.Items.Add("Продажа")
        params = { _
            { _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=room|Комнаты", _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=gostinka|Гостинка", _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=1|1-комнатные", _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=2|2-комнатные", _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=3|3-комнатные", _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=4|4-комнатные", _
                "d/realty/sell_flats/?city=371&agentType=privatePerson&flatType=5|5-комнатные" _
            } _
        }
        cboCatalog.SelectedIndex = 0

        lstConsole.Left = 0
        lstConsole.Dock = DockStyle.Fill
        web.ScriptErrorsSuppressed = True
        wbc.Encoding = System.Text.Encoding.GetEncoding("windows-1251")
    End Sub


    Private Sub cboCatalog_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCatalog.SelectedIndexChanged
        Dim n As Integer, value As String
        cboKomnat.Items.Clear()
        For n = 0 To 6
            value = params(cboCatalog.SelectedIndex, n)
            If value <> "" Then _
                cboKomnat.Items.Add(Split(value, "|")(1))
        Next
        cboKomnat.SelectedIndex = 0
    End Sub



    Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click
        Dim url As String
        Dim match As Match
        Dim p As Integer, n As Integer, max_page As Integer
        Dim response As String
        Dim tmpS As String

        btnRun.Enabled = False

        'Получить первую страницу
        max_page = 1
        url = SITE_URL & Split(params(cboCatalog.SelectedIndex, cboKomnat.SelectedIndex), "|")(0)
        Try : response = wbc.DownloadString(url)
        Catch ex As System.Net.WebException
            myConsole(lstConsole, "Страница не получена. " & ex.Message) : Exit Sub
        End Try

        'Удаляем все что ниже заголовка "Мы нашли больше предложений"
        n = response.IndexOf("<div class=""notificationPlate"">")
        If n > -1 Then
            myConsole(lstConsole, "В разделе найдено 'Мы нашли больше предложений'")
            response = response.Remove(n)
        Else
            'Найдем кол-во страниц
            match = Regex.Match(response, "<span class=""pagestat""> \((\d{1,5}) ")
            If match.Success = True Then
                n = CInt(match.Groups(1).Value)
                max_page = n \ 50
                If (n / 50) > max_page Then max_page += 1
            End If
        End If

        myConsole(lstConsole, "В разделе найдено " & max_page & " страниц")

        'В цикле проходим по всем страницам
        cur_adv = 0
        ReDim adverts(0)
        For p = 1 To max_page
            myConsole(lstConsole, "Загружаем " & p & "-ю страницу")
            Application.DoEvents()

            'Первую страницу мы уже получили
            If p > 1 Then
                Try : response = wbc.DownloadString(url & "&page=" & p)
                Catch ex As System.Net.WebException
                    myConsole(lstConsole, "Страница не получена. " & ex.Message) : Exit Sub
                End Try
            End If

            'Удаляем все что ниже заголовка "Архив объявлений"
            n = response.IndexOf("id='statusarchive'")
            If n > -1 Then
                myConsole(lstConsole, "В разделе найдено 'Архив объявлений'")
                response = response.Remove(n)
            End If

            'Получаем со страницы все объявления
            tmpS = "<td class=""descriptionCell"">.*?<a.*?href=""http://komsomolsk-na-amure.farpost.ru/(.*?)(\d{6,10}).html"
            For Each match In Regex.Matches(response, tmpS, RegexOptions.Singleline)
                ReDim Preserve adverts(cur_adv)
                adverts(cur_adv).url = match.Groups(1).Value & match.Groups(2).Value & ".html"
                adverts(cur_adv).advid = match.Groups(2).Value
                cur_adv = cur_adv + 1
            Next
        Next

        'Запускаем граббер
        h1 = "Farpost - " & cboCatalog.Text & " " & cboKomnat.Text
        timTick.Interval = 3000
        timTick.Start()
        cur_adv = 0
        txtCur.Text = 0
        txtMax.Text = adverts.Count
        myConsole(lstConsole, "Найдено " & adverts.Count & " объявлений")
        lstConsole.Items.Add("")
    End Sub



    Private Sub timTick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timTick.Tick
        Dim match As Match
        Dim response As String
        Dim tmpS As String, tmpS2 As String, tmpO As Object
        Dim n As Integer, m As Integer
        Dim flag As Boolean = False

        timTick.Stop()
        txtCur.Text = cur_adv + 1
        myConsole(lstConsole, (cur_adv + 1) & " объявление загружается")

        With adverts(cur_adv)
            'Проверим если объявление старое, то не загружаем его
            If chkOnlyNew.Checked = True Then
                If DBAdverts_isOldAdvert(ADV_NAME, .advid) = True Then
                    myConsole(lstConsole, "---Это объявление старое")
                    .state = AdvState.old
                    GoTo next_advert
                End If
            End If

            'Загружаем страницу
            Try : response = wbc.DownloadString(SITE_URL & .url)
            Catch ex As System.Net.WebException
                myConsole(lstConsole, "Страница не получена. " & ex.Message)
                Exit Sub
            End Try

            'Получаем номер телефона
            tmpS = ""
            For Each match In Regex.Matches(response, "<span class='phone'>(.*?)</span>")
                tmpS &= "," & match.Groups(1).Value
            Next
            tmpS = Regex.Replace(tmpS, " ", "")
            tmpS = Regex.Replace(tmpS, "-", "")
            If tmpS.StartsWith(",") Then tmpS = tmpS.Substring(1)
            'Если нет телефона, эта объява не нужна
            If tmpS = "" Then
                .state = AdvState.bad
                GoTo next_advert
            End If

            .phone = getPhonesArray(tmpS)

            'Ищем телефон в риэлторах
            For n = 0 To UBound(.phone)
                If DBEstate_isEstate(.phone(n), True) = True Then
                    myConsole(lstConsole, "---Это риэлтор")
                    .state = AdvState.estateInDB
                    'Сохраняем объявление с риэлтором как старое
                    If DBAdverts_isOldAdvert(ADV_NAME, .advid) = False Then
                        DBAdverts_addOldAdvert(ADV_NAME, .advid)
                    End If
                    'Переходим к следующему объявлению
                    GoTo next_advert
                End If
            Next

            'Получаем данные объявления
            .state = AdvState.normal

            'Адрес
            match = Regex.Match(response, "<span class=""inplace"" data-field=""street-buildingId"">(.*?)</span>", RegexOptions.Singleline)
            If match.Success = True Then
                .street = match.Groups(1).Value.Trim()
                tmpO = .street.Split(",")
                If UBound(tmpO) = 1 Then
                    .street = tmpO(0).ToString.Trim
                    .street = GetNormStreet(.street, True, {"ул", "пр-кт", "пр-т", "пер"})
                    .street = GetNormStreet(.street, False, {"ул", "пр-кт", "пр-т", "пер"})
                    .nomer = tmpO(1).ToString.Trim
                End If
            End If

            'Общая площадь
            match = Regex.Match(response, "<span class=""inplace"" data-field=""areaTotal"">(.*?)</span>", RegexOptions.Singleline)
            If match.Success = True Then
                .kvm_all = match.Groups(1).Value
                .kvm_all = Regex.Replace(.kvm_all, "кв. м.", "")
                .kvm_all = .kvm_all.Trim()
                .AddText("общ. " & .kvm_all & "кв.м")
            End If

            'Цена
            match = Regex.Match(response, "<span class=""inplace"" data-field=""price"">(.*?)</span>", RegexOptions.Singleline)
            If match.Success = True Then
                tmpS = match.Groups(1).Value
                tmpS = tmpS.Replace(" ", "").Replace("&nbsp;", "").Trim()
                match = Regex.Match(tmpS, "р\.")
                If match.Success = True Then
                    tmpS2 = match.Groups(0).Value
                    tmpS = tmpS.Replace(tmpS2, "").Trim()
                Else
                    tmpS2 = ""
                End If
                If Val(tmpS) > 99000 And tmpS.EndsWith("000") Then
                    tmpS = tmpS.Substring(0, tmpS.Length - 3)
                    .AddText(tmpS & "тыс." & tmpS2)
                Else
                    .AddText(tmpS & " " & tmpS2)
                End If
                .price = tmpS
            End If

            'Текст
            match = Regex.Match(response, "<p class=""inplace"" data-field=""text"">(.*?)</p>", RegexOptions.Singleline)
            tmpS = match.Groups(1).Value
            tmpS = Regex.Replace(tmpS, "[\r\n]", "")
            tmpS = Regex.Replace(tmpS, "&quot;", """")
            tmpS = Regex.Replace(tmpS, "<br />", " ")
            tmpS = Regex.Replace(tmpS, "  ", " ")
            tmpS = tmpS.Trim()
            .AddText(tmpS)
            .AddContent(tmpS)

next_advert:
            lstConsole.Items.Add("")
            If cur_adv = adverts.Count - 1 Then
                'Объявления кончились, выводим их
                grbControl.Enabled = False
                lstConsole.Visible = False
                web.Visible = True
                web.Left = 0
                web.Dock = DockStyle.Fill
                
                'Подготавливаем список перед выводом
                AdvertsCreateList(adverts, chkSaveNew.Checked, ADV_NAME)

                'Собираем HTML код и обновляем WebBrowser
                AdvertsShowList(web, adverts, h1, AddressOf SpanPhoneClick, ADV_NAME)
            Else
                'Смотрим следующее объявление
                If .state = AdvState.old Then 'Если это старая объява, то не ждем
                    timTick.Interval = 100
                Else 'Ждем 1.5 сек
                    timTick.Interval = 3000
                End If
                timTick.Start()
            End If
        End With
        cur_adv = cur_adv + 1
    End Sub



    Private Sub SpanPhoneClick(ByVal sender As Object, ByVal e As System.EventArgs)
        AdvertsSpanPhoneClick(web, adverts, h1, AddressOf SpanPhoneClick, ADV_NAME)
    End Sub



    Public Function getAdverts() As Object
        Return adverts
    End Function
End Class